﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
namespace GUI.PhieuXuatHuy
{
    public partial class frmXacNhan : Form
    {
        #region Cap nhap giao diện main
        public delegate void GetBoolean(Boolean capnhat);
        public GetBoolean GetData;
        #endregion

        #region Delegate form xử lý
        public void GetValue(Boolean capnhat)
        {
            if (capnhat == true)
            {
                GUI.PhieuXuatHuy.frmXuLy.CheckClose = false;
                HienThi(makho);
            }
        }
        #endregion

        #region Khởi tạo
        private Entities.PhieuXuatHuy[] DanhSach;
        private Entities.PhieuXuatHuy PhieuXuatHuy = null;
        private string MaPhieuXuatHuy = string.Empty;
        private Boolean CheckClose = false;
        Entities.PhieuXuatHuy[] pxhuy;
        string makho = "";
        public frmXacNhan()
        {
            InitializeComponent();
            Entities.CaiDatKho kho = GUI.Entitystatic.ViewFile();
            makho = kho.MaKho;
        }
        #endregion

        #region Processing
        //lay ban ghi de sua hoac xem
        private void GetRow()
        {
            try
            {
                if (DanhSach == null) { return; }
                for (int i = 0; i < this.DanhSach.Length; i++)
                {
                    if (DanhSach[i].MaPhieuXuatHuy == this.MaPhieuXuatHuy)
                    {
                        PhieuXuatHuy = DanhSach[i];
                        break;
                    }
                    else
                    { continue; }
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion

        #region Thoát
        private void Thoat()
        {
            try
            {
                System.Windows.Forms.DialogResult ketqua = System.Windows.Forms.MessageBox.Show("Bạn có chắc chắn muốn thoát không ?", "Thông báo", System.Windows.Forms.MessageBoxButtons.YesNo);
                {
                    if (ketqua == System.Windows.Forms.DialogResult.Yes)
                    {
                        this.CheckClose = true;
                        this.Close();
                    }
                }
            }
            catch (Exception)
            { }
        }
        #endregion

        #region Event

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Thoat();
        }

        private void frmQuanLyPhieuXuatHuy_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                if (CheckClose == false)
                {
                    System.Windows.Forms.DialogResult ketqua = System.Windows.Forms.MessageBox.Show("Bạn có chắc chắn muốn thoát không ?", "Thông báo", System.Windows.Forms.MessageBoxButtons.YesNo);
                    {
                        if (ketqua == System.Windows.Forms.DialogResult.Yes)
                        {
                            e.Cancel = false;
                        }
                        else
                        {
                            e.Cancel = true;
                        }
                    }
                }
                GetData(true);
            }
            catch (Exception)
            { }
        }
        #endregion

        #region HienThi
        private void HienThi(string mk)
        {
            try
            {
                Entities.PhieuXuatHuy nv = new Entities.PhieuXuatHuy();
                nv.HanhDong = "3";
                Entities.CaiDatKho kho = GUI.Entitystatic.ViewFile();
                mk = kho.MaKho;
                DanhSach = Common.StaticVariable.InterfaceObject.SelectPhieuXuatHuy1(nv);                
                dgvHienThi.DataSource = DanhSach;
                this.HienThiListview();
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                DanhSach = null;
                HienThiListview();
            }
        }

        private void HienThiListview()
        {
            try
            {

                if (DanhSach == null)
                {
                    Entities.PhieuXuatHuy[] nv = new Entities.PhieuXuatHuy[0];
                    dgvHienThi.DataSource = nv;
                }
                for (int i = 0; i < dgvHienThi.ColumnCount; i++)
                {
                    dgvHienThi.Columns[i].Visible = false;
                }
               
                dgvHienThi.Columns["HanhDong"].HeaderText = "STT";
                dgvHienThi.Columns["HanhDong"].Visible = true;
                dgvHienThi.Columns["NgayLap"].HeaderText = "ngày lập";
                dgvHienThi.Columns["NgayLap"].Visible = true;
                dgvHienThi.Columns["MaKho"].HeaderText = "Mã kho";
                dgvHienThi.Columns["MaKho"].Visible = true;
                dgvHienThi.Columns["Tongtien"].HeaderText = "Tổng tiền";
                dgvHienThi.Columns["Tongtien"].Visible = true;
                dgvHienThi.Columns["TrangThai"].HeaderText = "Trạng thái";
                dgvHienThi.Columns["TrangThai"].Visible = true;
                if (dgvHienThi.RowCount > 0)
                {
                    Common.Utilities.CountDatagridview(dgvHienThi);
                    dgvHienThi.Focus();
                    dgvHienThi.Select();
                }
                dgvHienThi.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                dgvHienThi.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion

        #region Check Permission
        #region Check Permission
        public bool CheckPer(int hanhDong)
        {
            return Common.Utilities.Check(Common.FormName.XacNhanPhieuXuatHuy, hanhDong);
        }
        #endregion
        #endregion

        #region Xác nhận
        void XacNhan(Entities.PhieuXuatHuy pxh)
        {
            // check per
            // check per
            if (!CheckPer(0))
            {
                MessageBox.Show(Common.MSG.MSG10(), Common.MSG.TieuDe);
                return;
            }
            DanhSach = (Entities.PhieuXuatHuy[])dgvHienThi.DataSource;
            // check object
            if (DanhSach.Length == 0 || DanhSach == null)
            {
                  MessageBox.Show(Common.MSG.MSG20(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                return;
            }
            // check mess
            if (MessageBox.Show(Common.MSG.MSG32(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
            {
                return;
            }
            if (!CheckXacNhan(DanhSach))
            {
                 MessageBox.Show("Bạn chưa chọn Hóa đơn cần xác nhận?", Common.MSG.TieuDe, MessageBoxButtons.OK);
                return;
            }
            // check xác nhận
            if ( Common.StaticVariable.InterfaceObject.InsertUpdatePhieuXuatHuy(PhieuXuatHuy) == 0)
            {

                return;
            }

            this.DanhSach1();
            //BusinessLogic.PhieuXuatHuy.TruKho(chitietkho);
            Common.StaticVariable.InterfaceObject.TruKhoPhieuXuatHuy(chitietkho);
            PhieuXuatHuy = null;
            DanhSach = (Entities.PhieuXuatHuy[])dgvHienThi.DataSource;
        }

        public Entities.ChiTietKhoHang[] chitietkho = null;
        ArrayList list = null;
        public void DanhSach1()
        {
            list = new ArrayList();
            try
            {
                Entities.PhieuXuatHuy[] hoadon = (Entities.PhieuXuatHuy[])dgvHienThi.DataSource;
                for (int i = 0; i < hoadon.Length; i++)
                {
                    if (hoadon[i].HanhDong == "Xacnhan")
                    {
                        this.LayDanhSach(hoadon[i]);
                    }
                }
                int l = list.Count;
                if (l <= 0)
                {
                    chitietkho = null;
                }
                else
                {
                    chitietkho = new Entities.ChiTietKhoHang[l];
                    for (int i = 0; i < l; i++)
                    {
                        chitietkho[i] = (Entities.ChiTietKhoHang)list[i];
                    }
                }
            }
            catch (Exception)
            {
            }
        }
        Entities.ChiTietXuatHuy nv;
        public void LayDanhSach(Entities.PhieuXuatHuy row)
        {
            try
            {
                Entities.ChiTietXuatHuy[] values = Common.StaticVariable.InterfaceObject.SelectChiTietXuatHuy1(nv);
                for (int i = 0; i < values.Length; i++)
                {
                    if (row.MaPhieuXuatHuy == values[i].MaPhieuXuatHuy)
                    {
                        Entities.ChiTietKhoHang r = new Entities.ChiTietKhoHang();
                        r.HanhDong = "Insert";
                        r.MaKho = Entitystatic.ViewFile().MaKho;
                        r.MaHangHoa = values[i].MaHangHoa;
                        r.TenHangHoa = values[i].TenHangHoa;
                        r.SoLuong = values[i].SoLuong.ToString();
                        this.list.Add(r);
                    }
                }

            }
            catch (Exception)
            {
            }
        }
        bool CheckXacNhan(Entities.PhieuXuatHuy[] hienthi)
        {
            try
            {
                bool kt = false;
                ArrayList array = new ArrayList();
                foreach (Entities.PhieuXuatHuy item in hienthi)
                {
                    if (item.TrangThai)
                    {
                        item.HanhDong = "Xacnhan";
                        array.Add(item);
                        kt = true;
                    }
                }
                DanhSach = (Entities.PhieuXuatHuy[])array.ToArray(typeof(Entities.PhieuXuatHuy));
                return kt;
            }
            catch
            {
                return false;
            }
        }
        #endregion
        private void dgvHienThi_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvHienThi.Rows.Count <= 0)
            { return; }
            MaPhieuXuatHuy = dgvHienThi.CurrentRow.Cells["MaPhieuXuatHuy"].Value.ToString();
            this.GetRow();
        }
        private void frmQuanLy_Load(object sender, EventArgs e)
        {
            this.HienThi(makho);
        }

        private void btnXacNhan_Click(object sender, EventArgs e)
        {
            Entities.PhieuXuatHuy[] pxh = (Entities.PhieuXuatHuy[])dgvHienThi.DataSource;
            if (pxh == null)
            {
                return;
            }
            for (int i = 0; i < pxh.Length; i++)
            {
                if (pxh[i].TrangThai)
                {
                    XacNhan(pxh[i]);
                    HienThi(makho);
                }
            }
        }
        #region Tìm
        public void TimKiem()
        {
            dgvHienThi.DataSource = (Entities.PhieuXuatHuy[])DanhSach.Where(p => p.MaPhieuXuatHuy.ToUpper().IndexOf(txtTimKiem.Text.ToUpper()) >= 0).ToArray();
            HienThiListview();
        }
        #endregion

        private void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            TimKiem();
        }

        private void btnTim_TextChanged(object sender, EventArgs e)
        {
            TimKiem();
        }

    }
}
